/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.messaging.inbox;

import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import nl.cloudfarming.client.messaging.Message;
import org.openide.nodes.NodeEvent;
import org.openide.nodes.NodeMemberEvent;
import org.openide.nodes.NodeReorderEvent;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.explorer.ExplorerManager;
import org.openide.nodes.Node;
import org.openide.nodes.NodeListener;
import org.openide.util.Lookup;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//nl.cloudfarming.client.messaging.inbox//Messages//EN",
autostore = false)
@TopComponent.Description(preferredID = "MessagesTopComponent",
iconBase = "nl/cloudfarming/client/icon/mail-message-new.png",
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "properties", openAtStartup = true)
@ActionID(category = "Window", id = "nl.cloudfarming.client.messaging.inbox.MessagesTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(displayName = "#CTL_MessagesAction",
preferredID = "MessagesTopComponent")
public final class MessagesTopComponent extends TopComponent implements ExplorerManager.Provider, NodeListener {

    private ExplorerManager explorerManager;
//    private OutlineView view = new OutlineView();
    private JScrollPane scrollPane = new JScrollPane();
    private MessagesPanel messagesPanel = new MessagesPanel();
    private boolean initialized = false;

    public MessagesTopComponent() {
        setName(NbBundle.getMessage(MessagesTopComponent.class, "CTL_MessagesTopComponent"));
        setToolTipText(NbBundle.getMessage(MessagesTopComponent.class, "HINT_MessagesTopComponent"));
        MessageInbox inbox = Lookup.getDefault().lookup(MessageInbox.class);
        explorerManager = inbox.getExplorerManager();
        associateLookup(inbox.getLookup());

        setLayout(new BorderLayout());
        add(scrollPane.getViewport());
        scrollPane.setViewportView(messagesPanel);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
//        view.setPropertyColumns("imageIcon","imageIcon","image","image","title","title","messageText","messageText");


    }

    @Override
    public void componentOpened() {
        explorerManager.getRootContext().addNodeListener(this);
        if (!initialized) {
            for (Node node : explorerManager.getRootContext().getChildren().getNodes()) {
                Message message = node.getLookup().lookup(Message.class);
                if (message != null) {
                    messagesPanel.addMessage(message);
                }
            }
            initialized = true;
        }
    }

    @Override
    public void componentClosed() {
        explorerManager.getRootContext().removeNodeListener(this);
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    @Override
    public ExplorerManager getExplorerManager() {
        return explorerManager;
    }

    @Override
    public void childrenAdded(NodeMemberEvent ev) {
        for (Node node : ev.getDelta()) {
            Message message = node.getLookup().lookup(Message.class);
            if (message != null) {
                messagesPanel.addMessage(message);
            }
        }
    }

    @Override
    public void childrenRemoved(NodeMemberEvent ev) {
    }

    @Override
    public void childrenReordered(NodeReorderEvent ev) {
    }

    @Override
    public void nodeDestroyed(NodeEvent ev) {
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
    }
}
